package com.swfee.qinben;


import android.content.Intent;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import com.swfee.qinben.base.AppBaseActivity;
import com.swfee.qinben.service.AppLogService;



public class AppInstallFirstActivity extends AppBaseActivity implements
		OnTouchListener {

	private GestureDetector gestureDetector;

	private Animation slideLeftIn;
	private Animation slideLeftOut;
	private Animation slideRightIn;
	private Animation slideRightOut;

	private ViewFlipper viewFlipper;

	private static final int SWIPE_MIN_DISTANCE = 120;
	private static final int SWIPE_THRESHOLD_VELOCITY = 200;
	private int viewIndex = 1;

	// private int FP = ViewGroup.LayoutParams.FILL_PARENT;

	private Matrix mSuppMatrix = new Matrix();
	private Matrix mBaseMatrix = new Matrix();
	private Matrix mDisplayMatrix = new Matrix();


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_install_first_view);

		gestureDetector = new GestureDetector(this, new MyGestureDetector());

		viewFlipper = (ViewFlipper) findViewById(R.id.app_start_flipper);
		slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.push_left_in);
		slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.push_left_out);
		slideRightIn = AnimationUtils.loadAnimation(this, R.anim.push_right_in);
		slideRightOut = AnimationUtils.loadAnimation(this,
				R.anim.push_right_out);

		// LayoutParams params = new ViewGroup.LayoutParams(FP,FP);
//		int index = 0;
//		for (Integer i : images) {
//
//			LinearLayout llLayout = new LinearLayout(this);
//			llLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//			llLayout.setBackgroundDrawable(getResources().getDrawable(i));
//			viewFlipper.addView(llLayout, index);
//			index++;
//		}
		viewFlipper.setOnTouchListener(this);
		viewFlipper.setLongClickable(true);
		
		
		View btnAppStart = findViewById(R.id.btnAppStart);
		btnAppStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				toAppStart();
			}
		});
		
		

	}
	
	public void toAppStart(){
		viewFlipper.stopFlipping();
		viewFlipper.removeAllViews();
		viewFlipper.setVisibility(View.GONE);
		
		//记录第一次安装提示已过
		AppLogService appLogService = new AppLogService(AppInstallFirstActivity.this);
		appLogService.logFirst();
		Intent intent = new Intent();
		intent.setClass(AppInstallFirstActivity.this, AppLoginActivity.class);
		startActivityBeforeFinish(intent);
		
	}

	class MyGestureDetector extends SimpleOnGestureListener {

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			
			if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				
				if(viewIndex == viewFlipper.getChildCount()){
					toAppStart();
					return false;
					
				}
				
				viewFlipper.setInAnimation(slideRightIn);
				viewFlipper.setOutAnimation(slideLeftOut);
				viewFlipper.showNext();
				viewIndex ++;
				
			} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
					&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
				viewFlipper.setInAnimation(slideLeftIn);
				viewFlipper.setOutAnimation(slideRightOut);
				viewFlipper.showPrevious();
				viewIndex --;
			}

			return false;
		}

		@Override
		public void onLongPress(MotionEvent e) {
			ImageView iv = (ImageView) viewFlipper.getCurrentView();
			// mSuppMatrix.postTranslate(2, 2);
			mDisplayMatrix.set(mBaseMatrix);
			mDisplayMatrix.postConcat(mSuppMatrix);
			iv.setImageMatrix(mDisplayMatrix);

//			isZoom = true;
		}

	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return gestureDetector.onTouchEvent(event);
	}

//	private Integer[] images = { R.drawable.app_install_1,
//			R.drawable.app_install_2, R.drawable.app_install_3 };
}